//
//  HATips2PopView.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HATips2PopView.h"

@interface HATips2PopView ()

@property (nonatomic, strong) NSString *titleText;
@property (nonatomic, strong) NSString *contentText;

/***/
@property (nonatomic, strong) UILabel *titLabel;
/***/
@property (nonatomic, strong) UILabel *conLabel;
/***/
@property (nonatomic, strong) UIButton *rightButton;
/***/
@property (nonatomic, strong) UIView *line1View;

@end

@implementation HATips2PopView

-(instancetype)initWithTitle:(NSString *)title content:(NSString *)content {
    if (self = [super init]) {
        
        self.contentText = content;
        self.titleText = title;
        [self layoutViews];
    }
    return self;

}

-(void)buttomButtonClick:(UIButton *)button {
    [self dismiss];
    if (self.XZYGTiShiSuretBlock) {
        self.XZYGTiShiSuretBlock();
    }
}

-(void)layoutViews {
    
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
    
    CGFloat alertViewW = factor_w(240);
    CGFloat alertViewH = 0;
    
    //计算文字内容高度
    NSDictionary *attrs = @{NSFontAttributeName :[UIFont systemFontOfSize:14]};
    CGSize maxSize = CGSizeMake(alertViewW-24, MAXFLOAT);
    NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    
    CGFloat other = 42;
    other += 50;
    other += 20;

    // 计算文字占据的宽高
    CGSize textSize = [self.contentText boundingRectWithSize:maxSize options:options attributes:attrs context:nil].size;
    if (textSize.height+10+other < factor_w(150)) {
        alertViewH = factor_w(150);
    }else if (textSize.height+10+other > kScreenHeight*0.65) {
        alertViewH = kScreenHeight*0.65;
    }else {
        alertViewH = textSize.height+10+other;
    }
    
    CGFloat alertViewX = (kUIScreen_w-alertViewW)*0.5;
    CGFloat alertViewY = (kUIScreen_h-alertViewH)*0.5;
    self.alertViewY = alertViewY;
    self.alertView.frame = CGRectMake(alertViewX, alertViewY, alertViewW, alertViewH);
    [self addSubview: self.alertView];
    self.alertView.layer.cornerRadius = 5;
    self.alertView.layer.masksToBounds = YES;
    
    [self.alertView addSubview:self.titLabel];
    self.titLabel.text = self.titleText;
    [self.titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
        make.height.mas_equalTo(15);
    }];
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.alertView addSubview:scrollView];
    scrollView.frame = CGRectMake(0, 50, alertViewW, alertViewH-other-2);
    scrollView.contentSize = CGSizeMake(0, textSize.height+20);
    
    [self.alertView addSubview:self.line1View];
    [self.line1View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_offset(0.5);
        make.bottom.mas_equalTo(-42);
    }];
    
    [self.alertView addSubview:self.rightButton];
    [self.rightButton setTitle:@"我知道了" forState:UIControlStateNormal];
    [self.rightButton setTitleColor:[UIColor colorWithHexString:@"#34A0FF"] forState:0];
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.line1View.mas_bottom);
        make.left.mas_equalTo(0);
    }];
    
    self.conLabel.text = self.contentText;
    [scrollView addSubview:self.conLabel];
    self.conLabel.frame = CGRectMake(12, 0, scrollView.frame.size.width-24, textSize.height+10);
}

- (UILabel *)titLabel {
    if (!_titLabel) {
        _titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#303133" text:@"" alignment:1];
    }
    return _titLabel;
}

- (UILabel *)conLabel
{
    if (!_conLabel) {
        _conLabel = [UILabel ha_NLabelWithFont:14 textColor:@"#494245" text:@"" alignment:1];
        _conLabel.numberOfLines = 0;
    }
    return _conLabel;
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_rightButton addTarget:self action:@selector(buttomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

#pragma mark - 分割线
- (UIView *)line1View {
    if (!_line1View) {
        _line1View = [UIView ha_createView];
        _line1View.backgroundColor  = [UIColor colorWithHexString:@"#C0C4CC"];
    }
    return _line1View;
}


@end
